package cn.akwing.sms.controller;

import cn.akwing.sms.pojo.Admin;
import cn.akwing.sms.pojo.Student;
import cn.akwing.sms.pojo.Teacher;
import cn.akwing.sms.pojo.Text;
import cn.akwing.sms.service.TextService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author FRSF
 * 2022年2月1日16点54分
 * 留言板功能
 */

@RestController
@RequestMapping(value = "/text")
public class TextController {
    @Autowired
    private TextService textService;

    @RequestMapping(value = "/getUserName")
    public Object getUserName(HttpSession session){
        return session.getAttribute("userInfo");
    }

    @RequestMapping(value = "/insertText")
    public void insertText(@RequestBody String Data, HttpSession session){
        Admin admin;
        Teacher teacher;
        Student student;
        Text text = new Text();
        if((Integer)session.getAttribute("userType")==1){
            admin = (Admin) session.getAttribute("userInfo");
            text.setId(admin.getId());
            text.setName(admin.getName());
        }else if((Integer)session.getAttribute("userType")==2){
            teacher = (Teacher) session.getAttribute("userInfo");
            text.setId(teacher.getId());
            text.setName(teacher.getName());
        }else {
            student = (Student) session.getAttribute("userInfo");
                text.setId(student.getId());
            text.setName(student.getName());
        }
        text.setDateTime(LocalDateTime.now());
        text.setText(Data);
        System.out.println(text);
        textService.insert(text);
    }

    @RequestMapping(value = "/getAllText")
    public List<Text> getAllText(){
        return textService.selectAll();
    }
}
